package com.dream.sys.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dream.commons.result.PageInfo;
import com.dream.commons.utils.JsonUtils;
import com.dream.commons.utils.StringUtils;
import com.dream.sys.base.BaseController;
import com.dream.sys.model.Role;
import com.dream.sys.model.User;
import com.dream.sys.model.DTO.LayuiTreeDto;
import com.dream.sys.model.DTO.userDto.ChangePwdReqDto;
import com.dream.sys.model.DTO.userDto.UserDto;
import com.dream.sys.model.DTO.userDto.UserListReqDto;
import com.dream.sys.service.OrganizationService;
import com.dream.sys.service.RoleService;
import com.dream.sys.service.UserService;
import com.dream.sys.shiro.PasswordHash;
import com.dream.sys.shiro.ShiroUser;


/**
 * 
 * @description：用户管理
 * @author zhouy
 * @date 2018-2-14
 */

@Controller
@RequestMapping("/user")
public class UserInfoController  extends BaseController {

 @Autowired
 private UserService userService;
 @Autowired
 private PasswordHash passwordHash;
 @Autowired
 private RoleService roleService;
 @Autowired
 private OrganizationService organizationService;
	/**
	 * 修改密码页面
	 * @return
	 */
    @GetMapping("/changePwdPage")
    public String changePwdPage() {
        return "common/changePassword";
    } 
    
    /**
     * 用户列表页面
     * @return
     */
    
    @GetMapping("/manager")
    public String manager() {
        return "admin/userList";
    }
    
    /**
	 * 个人用户信息页面
	 * @return
	 */
    @GetMapping("/detailUserPage")
    public String detailUserPage(Model model) {
    	ShiroUser shiroUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
    	Long id = shiroUser.getId();
    	UserDto userDto = userService.selectDtoById(id);       
        model.addAttribute("user", userDto);
        return "common/detailUser";
    }
	  /**
	   * 修改个人密码
	   * @param changePwdReqDto
	   * @return
	   */
    @PostMapping("/changePwd")
    @ResponseBody
    public Object changePwd(ChangePwdReqDto changePwdReqDto) {               
    	String msg = userService.changePwd(changePwdReqDto);
        return renderSuccess(msg);
    }
    
     /**
      * 
      * @param sysLogReqDto
      * @return
      */
    @GetMapping("/dataGrid")
    @ResponseBody
    public PageInfo<User> dataGrid(UserListReqDto userListReqDto) {     
    	PageInfo<User> page =  userService.selectDataGrid(userListReqDto);
        return page;
    }
    
    /**
     * 删除log
     * @param
     * @return
     */
    @PostMapping(value = "del")
    @ResponseBody
    public Object del(String[] ids){
    	String msg="删除成功";
    	userService.deleteUsers(ids);
        return renderSuccess(msg);
    }
    
    /**
     * 新增用户页面
     * @return
     */
    @GetMapping("/addUserPage")
    public String addUser(Model model) {  
    	List<Role> roleList = roleService.selectAll();
        model.addAttribute("roleList",roleList);
        List<LayuiTreeDto> orgTree = organizationService.selectLayuiTree();
        model.addAttribute("orgTree",JsonUtils.toJson(orgTree));
        return "admin/addUser";
    }
    
    /**
     * 添加用户
     *
     * @param userDto
     * @return
     */
    @PostMapping("/addUser")
    @ResponseBody
    public Object addUser(@RequestBody UserDto userDto) {
        List<User> list = userService.selectByLoginName(userDto);
        if (list != null && !list.isEmpty()) {
            return renderError("登录名已存在!");
        }
        String salt = StringUtils.getUUId();
        String pwd = passwordHash.toHex(userDto.getPassword(), salt);
        userDto.setSalt(salt);
        userDto.setPassword(pwd);
        userService.insertByDto(userDto);
        return renderSuccess("添加成功");
    }
}
